/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-dealer
*
* @File name : GetCarService.java
*
* @Author : Administrator
*
* @Date : 2017年12月5日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月5日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.dealer.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.service.dealer.dao.TmUsersMapper;
import com.yonyou.gmmc.service.dealer.dao.TtEtcoMapper;
import com.yonyou.gmmc.service.dealer.model.TmUsers;
import com.yonyou.gmmc.service.dealer.model.TtEtco;

/**
*
* @author Administrator
* 企业号上门取送车API
* @date 2017年12月5日
*/

@Service
@Transactional
public class GetCarService {

    private Logger logger = Logger.getLogger(GetCarService.class);
    
    @Autowired
    private TmUsersMapper tmUsersMapper;
    
    @Autowired
    private TtEtcoMapper ttEtcoMapper;
    
    @Autowired
    private GetCarServiceInterface getCarServiceInterface;
    
    /**
     * 判断是否存在该用户
     * @author QuanKK
     * @date 2018年1月24日 上午11:01:54
     * @param phone
     * @return
     */
    public List<TmUsers> queryTmUsersList(String phone){
		try{
        	List<TmUsers> tmUsersList = tmUsersMapper.queryTmUsersList(phone);
            return tmUsersList;
		}catch (Exception e){
	        logger.error(e.getMessage());
	        throw new ServiceBizException(e.getMessage(), e);
	    }
	}

	/**
	 * 查询取送车列表
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:02:21
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	public PageInfo<TtEtco> queryEtcoList(Map<String, Object> paramMap) throws Exception{
		List<TtEtco> resultList = new ArrayList<TtEtco>();
		try{
			//判断售后顾问是否存在
        	String phone = paramMap.get("phone").toString();
        	logger.info("售后顾问phone="+phone);
        	List<TmUsers> tmUsersList = queryTmUsersList(phone);
        	if(CheckUtil.NullOrEmpty(tmUsersList)){
        		//用户不存在
        		throw new ServiceBizException("用户不存在！");
        	}else if(tmUsersList.size()>1){
        		//用户存在多条数据
        		throw new ServiceBizException("用户信息错误！");
        	}
        	String dealerCode = tmUsersList.get(0).getDealerCode();
        	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
        	paramMap.put("dealerCode", dealerCode);
        	resultList = ttEtcoMapper.queryEtcoList(paramMap);
        	Page<TtEtco> ss = PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()),
                    Integer.parseInt(paramMap.get("pageSize").toString()));
        	PageInfo<TtEtco> page = new PageInfo<TtEtco>(resultList);
			return page;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}

	/**
	 * 查询取送车详情
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:02:41
	 * @param pkEtcoId
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	public TtEtco queryEtcoDetail(Integer pkEtcoId, String phone) throws Exception{
		TtEtco ttEtco = new TtEtco();
		try{
			//判断售后顾问是否存在
        	logger.info("售后顾问phone="+phone);
        	List<TmUsers> tmUsersList = queryTmUsersList(phone);
        	if(CheckUtil.NullOrEmpty(tmUsersList)){
        		//用户不存在
        		throw new ServiceBizException("用户不存在！");
        	}else if(tmUsersList.size()>1){
        		//用户存在多条数据
        		throw new ServiceBizException("用户信息错误！");
        	}
        	String dealerCode = tmUsersList.get(0).getDealerCode();
        	logger.info("售后顾问dealerCode="+dealerCode);
        	if(CheckUtil.NullOrEmpty(dealerCode)){
        		//经销商代码不存在
        		throw new ServiceBizException("用户信息错误！");
        	}
			logger.info("取送车pkEtcoId="+pkEtcoId);
			ttEtco = ttEtcoMapper.queryEtcoDetail(pkEtcoId,dealerCode);
			return ttEtco;
		}catch (Exception e){
            logger.error(e.getMessage());
            throw new ServiceBizException(e.getMessage(), e);
        }
	}
	
	/**
	 * 取消取送车
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:02:53
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> cancelGetSendCar(String etcoId,String phone){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		//判断售后顾问是否存在
    	logger.info("售后顾问phone="+phone);
    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
    	if(CheckUtil.NullOrEmpty(tmUsersList)){
    		//用户不存在
    		throw new ServiceBizException("用户不存在！");
    	}else if(tmUsersList.size()>1){
    		//用户存在多条数据
    		throw new ServiceBizException("用户信息错误！");
    	}
		Response<Map<String, Object>> rList = getCarServiceInterface.cancelCar(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	
	/**
	 * 查看进度
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:53:21
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> queryQscProgress(String etcoId,String phone){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		//判断售后顾问是否存在
    	logger.info("售后顾问phone="+phone);
    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
    	if(CheckUtil.NullOrEmpty(tmUsersList)){
    		//用户不存在
    		throw new ServiceBizException("用户不存在！");
    	}else if(tmUsersList.size()>1){
    		//用户存在多条数据
    		throw new ServiceBizException("用户信息错误！");
    	}
		Response<Map<String, Object>> rList = getCarServiceInterface.queryQscProgress(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	
	/**
	 * 查看轨迹信息
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:53:51
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> queryQscTrack(String etcoId,String phone){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		//判断售后顾问是否存在
    	logger.info("售后顾问phone="+phone);
    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
    	if(CheckUtil.NullOrEmpty(tmUsersList)){
    		//用户不存在
    		throw new ServiceBizException("用户不存在！");
    	}else if(tmUsersList.size()>1){
    		//用户存在多条数据
    		throw new ServiceBizException("用户信息错误！");
    	}
		Response<Map<String, Object>> rList = getCarServiceInterface.queryQscTrack(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	
	/**
	 * @author QuanKK
	 * @date 2018年1月24日 上午11:56:26
	 * @param etcoId
	 * @param phone
	 * @return
	 */
	public Map<String, Object> queryDriverinfo(String etcoId,String phone){
		logger.info("调用取送车服务  SERVICE-GETCAR==>start...");
		//判断售后顾问是否存在
    	logger.info("售后顾问phone="+phone);
    	List<TmUsers> tmUsersList = queryTmUsersList(phone);
    	if(CheckUtil.NullOrEmpty(tmUsersList)){
    		//用户不存在
    		throw new ServiceBizException("用户不存在！");
    	}else if(tmUsersList.size()>1){
    		//用户存在多条数据
    		throw new ServiceBizException("用户信息错误！");
    	}
		Response<Map<String, Object>> rList = getCarServiceInterface.queryDriverinfo(etcoId);
		Map<String, Object> result = new HashMap<>();
		if(rList!=null && rList.getResultCode()==1){
			result = rList.getData();
		}else if(rList!=null && rList.getResultCode()==0){
			throw new ServiceBizException(rList.getErrMsg());
		}else{
			throw new ServiceBizException("取送车服务异常");
		}
		return result;
	}
	
	
	
	
}
